/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNNspot.LiveChat.DataModel
{
    public partial class Agent
    {
        public static Agent Load(int? agentId)
        {
            var agent = new Agent();

            if (agentId != null)
                agent.LoadByPrimaryKey(Convert.ToInt32(agentId));

            return agent;
        }

        public static Agent LoadByUserId(int userId)
        {
            var q = new AgentQuery();
            q.Where(q.UserId == userId);

            var agent = new Agent();
            agent.Load(q);

            return agent;
        }

        public static bool IsAgent(int userId)
        {
            var q = new AgentQuery();
            q.Where(q.UserId == userId);

            var agents = new AgentCollection();
            agents.Load(q);

            return agents.Count > 0;
        }
    }

    public partial class AgentCollection
    {
        public static bool AgentIsAvailable(int portalId)
        {
            var q = new AgentQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.IsLoggedIn == true);

            var agents = new AgentCollection();
            agents.Load(q);

            return agents.Count > 0;
        }

        public static int GetTotal(FlexigridSearchCriteria searchCriteria)
        {
            var tmpNumber = searchCriteria.PageNumber;
            var tmpSize = searchCriteria.PageSize;

            searchCriteria.PageNumber = -1;
            searchCriteria.PageSize = -1;

            var count = Search(searchCriteria).Count;

            searchCriteria.PageNumber = tmpNumber;
            searchCriteria.PageSize = tmpSize;

            return count;
        }

        public static void RemoveAllInactive(int seconds)
        {
            var maxDate = DateTime.Now.AddSeconds(-seconds);
            var q = new AgentQuery();
            q.Where(q.ModifiedDate < maxDate);

            var agents = new AgentCollection();

            if (agents.Load(q))
                foreach (Agent agent in agents)
                    agent.IsLoggedIn = false;

            agents.Save();

        }

        public static List<Agent> Search(FlexigridSearchCriteria searchCriteria)
        {
            var agentQuery = new AgentQuery("a");
            var userQuery = new UserQuery("u");

            agentQuery.es.PageNumber = searchCriteria.PageNumber;
            agentQuery.es.PageSize = searchCriteria.PageSize;

            agentQuery.Select(userQuery.FirstName, userQuery.LastName, agentQuery);
            agentQuery.InnerJoin(userQuery).On(userQuery.UserID == agentQuery.UserId);
            agentQuery.Where(agentQuery.PortalId == searchCriteria.PortalId);

            foreach (String keyword in searchCriteria.Query.Split(' '))
            {
                agentQuery.And(string.Format("{0} LIKE '%{1}%'", searchCriteria.QueryColumns, keyword));
            }

            agentQuery.OrderBy(searchCriteria.SortColumn, searchCriteria.SortDirection);

            var agents = new AgentCollection();
            agents.Load(agentQuery);

            return agents.ToList();
        }
    }
}
